//
//  ReturnGoodsDetailViewController.m
//  LNH
//
//  Created by twentylx on 2018/2/24.
//  Copyright © 2018年 twentylx. All rights reserved.
//

#import "ReturnGoodsDetailViewController.h"
#import "CAReturnGoodsDetailHeaderView.h"
#import "CAReturnGoodsDetailGoodsView.h"
#import "CAReturnGoodsDetailStatusView.h"
#import "CAReturnGoodsDetailAddressView.h"
#import "CAReturnGoodsDetailSpeakView.h"
#import "CAReturnGoodsExpressViewController.h"

@interface ReturnGoodsDetailViewController ()

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIView *containerView;

@property (nonatomic, strong) CAReturnGoodsDetailHeaderView *headerView;
@property (nonatomic, strong) CAReturnGoodsDetailGoodsView *goodsView;
@property (nonatomic, strong) CAReturnGoodsDetailStatusView *statusView;
@property (nonatomic, strong) CAReturnGoodsDetailAddressView *addressView;
@property (nonatomic, strong) CAReturnGoodsDetailSpeakView *speakView;

@property (nonatomic, strong) UIButton *oneButton;
@property (nonatomic, strong) UIButton *twoButton;

@end

@implementation ReturnGoodsDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/// 初始化数据
- (void)lm_initialDefaultsForController {
}

/// 绑定 vm
- (void)lm_bindViewModelForController {
    
}

/// 创建视图
- (void)lm_createViewForController {
    [self.view setNeedsUpdateConstraints];
    
    [self.view addSubview:self.scrollView];
    [self.scrollView addSubview:self.containerView];
    
    [self.containerView addSubview:self.headerView];
    [self.containerView addSubview:self.goodsView];
    [self.containerView addSubview:self.statusView];
    [self.containerView addSubview:self.addressView];
    [self.containerView addSubview:self.speakView];
    [self.containerView addSubview:self.oneButton];
    [self.containerView addSubview:self.twoButton];
}

/// 配置导航栏
- (void)lm_configNavigationForController {
    self.navigationItem.title = @"退款详情";
}

#pragma mark layout
- (void)updateViewConstraints {
    [super updateViewConstraints];
    
    [_scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    [_containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.scrollView);
        make.width.equalTo(self.scrollView);
    }];
    
    [_headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.equalTo(self.containerView);
        make.top.equalTo(self.containerView);
        make.height.mas_equalTo(100);
    }];
    
    [_goodsView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.equalTo(self.containerView);
        make.top.equalTo(self.headerView.mas_bottom);
        make.height.mas_equalTo(120);
    }];
    
    [_statusView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.equalTo(self.containerView);
        make.top.equalTo(self.goodsView.mas_bottom);
        make.height.mas_equalTo(130);
    }];
    
    [_addressView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.equalTo(self.containerView);
        make.top.equalTo(self.statusView.mas_bottom);
        make.height.mas_equalTo(100);
    }];
    
    [_speakView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.equalTo(self.containerView);
        make.top.equalTo(self.addressView.mas_bottom);
        make.height.mas_equalTo(180);
    }];
    
//    [_oneButton mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(self.containerView).offset(50);
//        make.top.equalTo(self.speakView.mas_bottom).offset(20);
//        make.height.mas_equalTo(40);
//    }];
//
//    [_twoButton mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.right.equalTo(self.containerView).offset(-50);
//        make.top.equalTo(self.speakView.mas_bottom).offset(20);
//        make.height.mas_equalTo(40);
//    }];
    
    
    if (self.status == 1) {
        _goodsView.titleLabel.text = @"商家正在审核您发起的退款申请。";
        [_oneButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.containerView);
            make.top.equalTo(self.speakView.mas_bottom).offset(20);
            make.height.mas_equalTo(40);
        }];
        
        [[_oneButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            [self cancelReturnAction];
        }];
    } else if (self.status == 2) {
        _goodsView.titleLabel.text = @"商家已同意退货申请，请尽早退货。";
        
        [_oneButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.containerView).offset(50);
            make.top.equalTo(self.speakView.mas_bottom).offset(20);
            make.height.mas_equalTo(40);
        }];
        [[_oneButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            [self cancelReturnAction];
        }];

        [_twoButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.containerView).offset(-50);
            make.top.equalTo(self.speakView.mas_bottom).offset(20);
            make.height.mas_equalTo(40);
        }];
        [[_twoButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            CAReturnGoodsExpressViewController *expressVC = [CAReturnGoodsExpressViewController new];
            expressVC.dict = self.dict;
            [self.navigationController pushViewController:expressVC animated:YES];
        }];
    } else if (self.status == 3) {
        _goodsView.titleLabel.text = @"商家拒绝您发起的退款申请。";
    } else if (self.status == 4) {
        _goodsView.titleLabel.text = @"您的退款申请已完成。";
    } else if (self.status == 5) {
        _goodsView.titleLabel.text = @"您的退款申请已撤销。";
    }
    
    [_containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.oneButton.mas_bottom).offset(44);
    }];
    
}

#pragma mark get & set
- (UIScrollView *) scrollView {
    if (!_scrollView) {
        _scrollView = [UIScrollView new];
        _scrollView.backgroundColor = [UIColor clearColor];
    }
    return _scrollView;
}

- (UIView *) containerView {
    if (!_containerView) {
        _containerView = [UIView new];
        _containerView.backgroundColor = [UIColor clearColor];
    }
    return _containerView;
}

- (CAReturnGoodsDetailHeaderView *)headerView {
    if (!_headerView) {
        _headerView = [CAReturnGoodsDetailHeaderView new];
        _headerView.orderStatusLabel.text = self.dict[@"status_text"];
        _headerView.orderNoLabel.text = self.dict[@"refuse_content"];
    }
    return _headerView;
}

- (CAReturnGoodsDetailGoodsView *)goodsView {
    if (!_goodsView) {
        _goodsView = [CAReturnGoodsDetailGoodsView new];
        [_goodsView.goodsImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@", BASE_IMAGE_DOMAIN, self.dict[@"goods_img"]]]];
        _goodsView.nameLabel.text = self.dict[@"goods_name"];
        NSArray *attrArray = self.dict[@"attr"];
        __block NSString *attrStr = @"";
        [attrArray enumerateObjectsUsingBlock:^(NSDictionary *attrDict, NSUInteger idx, BOOL * _Nonnull stop) {
            attrStr = [NSString stringWithFormat:@"%@ %@:%@", attrStr, attrDict[@"name"], attrDict[@"attr_value"]];
        }];
        self.goodsView.sizeLabel.text = attrStr;
    }
    return _goodsView;
}

- (CAReturnGoodsDetailStatusView *)statusView {
    if (!_statusView) {
        _statusView = [CAReturnGoodsDetailStatusView new];
        _statusView.returnReason.text = [NSString stringWithFormat:@"退款原因：%@", self.dict[@"reason"]];
        
        NSString *total = [NSString stringWithFormat:@"%.2f", [self.dict[@"goods_price"] floatValue] * [self.dict[@"goods_number"] integerValue]];
        _statusView.returnPrice.text = [NSString stringWithFormat:@"退款金额：%@", total];
        _statusView.returnReason.text = [NSString stringWithFormat:@"申请时间：%@", self.dict[@"created_at"]];
        _statusView.returnNo.text = [NSString stringWithFormat:@"退款编号：%@", self.dict[@"return_sn"]];
    }
    return _statusView;
}

- (CAReturnGoodsDetailAddressView *)addressView {
    if (!_addressView) {
        _addressView = [CAReturnGoodsDetailAddressView new];
        _addressView.nameLabel.text = [NSString stringWithFormat:@"收货人：%@", _dict[@"company_consignee"]];
        _addressView.addressLabel.text = [NSString stringWithFormat:@"地址：%@", _dict[@"company_address"]];
        _addressView.phoneLabel.text = [NSString stringWithFormat:@"电话：%@", _dict[@"company_phone"]];
    }
    return _addressView;
}

- (CAReturnGoodsDetailSpeakView *)speakView {
    if (!_speakView) {
        _speakView = [CAReturnGoodsDetailSpeakView new];
    }
    return _speakView;
}

- (UIButton *)oneButton {
    if (!_oneButton) {
        _oneButton = [UIButton new];
        [_oneButton setTitle:@" 撤销申请 " forState:UIControlStateNormal];
        [_oneButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        _oneButton.titleLabel.font = [UIFont systemFontOfSize:16];
        _oneButton.layer.borderWidth = 1;
        _oneButton.layer.borderColor = [RGBCOLOR(75, 75, 75) CGColor];
        _oneButton.layer.cornerRadius = 5;
    }
    return _oneButton;
}

- (UIButton *)twoButton {
    if (!_twoButton) {
        _twoButton = [UIButton new];
        [_twoButton setTitle:@" 填写快递单号 " forState:UIControlStateNormal];
        [_twoButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        _twoButton.titleLabel.font = [UIFont systemFontOfSize:16];
        _twoButton.layer.borderWidth = 1;
        _twoButton.layer.borderColor = [RGBCOLOR(75, 75, 75) CGColor];
        _twoButton.layer.cornerRadius = 5;
    }
    return _twoButton;
}

#pragma mark private method
- (void) cancelReturnAction {
    
}

@end
